package com.yipao.admin.service.order;

import com.yipao.db.domain.goods.Goods;
import com.yipao.db.service.order.CartService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 购物车
 *
 * @author tianhao
 */
@Service
public class AdminCartService {
    @Resource
    private CartService cartService;

    /**
     * 异步删除购物车,通过购物车id
     *
     * @param goodsId
     */
    @Async
    public void asyncDeleteCartByGoodsId(Integer goodsId) {
        cartService.clearGoodsByGoodsId(goodsId);
    }

    /**
     * 异步修改购物车
     *
     * @param oldGoods
     * @param goods
     */
    @Async
    public void asyncCompareDeleteCart(Goods oldGoods, Goods goods) {
        if (this.compare(oldGoods, goods)) {
            cartService.clearGoodsByGoodsId(oldGoods.getId());
        }

    }

    /**
     * 比较值值是否相等
     *
     * @param oldGoods
     * @param goods
     * @return
     */
    private boolean compare(Goods oldGoods, Goods goods) {
        // 商品编号
        if (!oldGoods.getGoodsSn().equals(goods.getGoodsSn())) {
            return true;
        }
        // 商品名称
        if (!oldGoods.getName().equals(goods.getName())) {
            return true;
        }
        // 商品原料id
        if (!oldGoods.getFormulaId().equals(goods.getFormulaId())) {
            return true;
        }
        // 下架状态
        if (Goods.GOODS_OUT_STATUS.equals(goods.getIsOnSale())) {
            // 修改了状态为已下架
            if (!Goods.GOODS_OUT_STATUS.equals(oldGoods)) {
                return true;
            }
            return false;
        }
        return false;
    }
}
